﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DapAnDAL
/// </summary>
public class DapAnDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static int Insert(SqlTransaction trans, DapAnINFO obj)
    {
        // obj.MaCH = GetMa();
        // obj.MaCH = obj.MaDA + 1;
        string sql = "INSERT INTO DAPAN (MaDA,MaCH,NoiDungDA,GiaTri) VALUES (" + obj.MaDA + "," + obj.MaCH + ",N'" + obj.NoiDungDA + "'," + obj.GiaTri + ")";
        return SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
    }

    public static int GetMa()
    {
        string sql = "SELECT ISNULL(MAX(MaDA),0) FROM DAPAN";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        int ma = 0;
        if (dr.Read())
        {
            ma = int.Parse(dr[0].ToString());
        }
        dr.Close();
        return ma;
    }

    public static List<string> kiemtra(int macauhoi)
    {
        string sql = "select MaDA from DapAn WHERE MaCH = " + macauhoi + " AND GiaTri = 1";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        List<string> dsDA = new List<string>();
        while(dr.Read())
        {
             dsDA.Add(dr[0].ToString());
        }
        dr.Close();
        return dsDA;
    }

    public static List<DapAnINFO> listByCauHoi(int macauhoi)
    {
        string sql = "select * from DapAn WHERE MaCH = " + macauhoi;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<DapAnINFO>(dr);
    }

    public static List<DapAnINFO> listByCauHoi(string smacauhoi)
    {
        string sql = "select * from DapAn WHERE MaCH IN (" + smacauhoi + ")";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<DapAnINFO>(dr);
    }

    public static DapAnINFO GetInfo(int madapan)
    {
        string sql = "select * from DapAn where madapan =" + madapan + "";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<DapAnINFO>(dr);
    }

    
    public static int Update(DapAnINFO obj)
    {
        string sql = "Update DAPAN set NoiDungDA='" + obj.NoiDungDA + "' Where madapan=" + obj.MaDA + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Delete(SqlTransaction trans, int macauhoi)
    {
        string sql = "Delete from DAPAN where MaCH =" + macauhoi + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }


}